package stdio.costa.imoca.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import stdio.costa.imoca.model.Activity;
import stdio.costa.imoca.model.PostcardBack;

@Component
public class PostcardBackDao {

	@PersistenceContext
	private EntityManager entityManager;

	public void save(PostcardBack postcardBack) {
		this.entityManager.persist(postcardBack);
	}
	
	public PostcardBack findByUuid(String uuid) {
		Query query = this.entityManager.createNamedQuery(
				"findPostcardBacksByActivity").setParameter("uuid",
						uuid);
		try {
			return (PostcardBack) query.getSingleResult();
		} catch (NoResultException ne) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PostcardBack> findPostcardBacksByActivity(Activity activity) {
		Query query = this.entityManager.createNamedQuery(
				"findPostcardBacksByActivity").setParameter("activity",
						activity);
		try {
			return query.getResultList();
		} catch (NoResultException ne) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PostcardBack> findByTags(String hql) {
		Query query = this.entityManager.createQuery(hql);
		try {
			return query.getResultList();
		} catch (NoResultException ne) {
			return null;
		}
	}

}
